package com.keepmoving.web;

import com.keepmoving.po.Login;
import com.keepmoving.po.Order;
import com.keepmoving.po.User;
import com.keepmoving.service.OrderService;
import com.keepmoving.service.UserService;
import com.keepmoving.vo.CountUser;
import com.keepmoving.vo.CourseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *预约订单相关控制层
 * @author gyy
 */
@Controller
public class CourseOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    //查询订单所有集合,通过lId
    @RequestMapping("/toCourseOrder")
    public String CourseOrder(Model model,HttpSession session){
        Login login=(Login)session.getAttribute("currentLogin");
        int lId=login.getlId();
        List<CourseOrder> clList = orderService.findByCourseBylId(lId);
        System.out.println(clList.toString());
        model.addAttribute("clList",clList);
        return "courseOrderList";
    }
    //根据课程编号查新订单集合
    @RequestMapping("/findByCourseByCid")
    public String findByCourseByCid(Model model,int cId){
        List<CourseOrder> courseOrderList = orderService.findByCourseByCid(cId);
        model.addAttribute("courseOrderList",courseOrderList);
        return "courseOrderList";
    }
    //通过订单编号得到对应的订单
    @RequestMapping("/orderByOid")
    public String orderByOid(@ModelAttribute CourseOrder courseOrder){
        int i = courseOrder.getoId();
        int lid = courseOrder.getlId();
        Order order = orderService.findByoId(i);
        order.setoStatus("已预约");
        boolean flag = orderService.update(order);
        System.out.println(flag);
        System.out.println(lid);
        User user = userService.findById(lid);
        user.setNumber(user.getNumber()+1);
        userService.update(user);
        return "redirect:/toCourseOrder.action";
    }
    //
//查询订单所有集合
    @RequestMapping("/toCourseOrderInfo")
    public String CourseOrderInfo(Model model) {
        List<CourseOrder> list = new ArrayList<>();
        List<CourseOrder> ordersList = orderService.findByCourse();
        for (CourseOrder courseOrders : ordersList) {
            if (courseOrders.getoStatus().equals("已预约")) {
                list.add(courseOrders);
            }
        }
        System.out.println(ordersList.toString());
        model.addAttribute("ordersList", list);
        return "courseOrderInfoList";
    }

    //统计教练对应的学员数
    //页面跳转
    @RequestMapping("/tocoachuser")
    public String tocoachuser() {
        return "coachuserinfo";
    }

    @RequestMapping("/coachuser")
    @ResponseBody
    public List<CountUser> coachuser() {
        List<CountUser> list = orderService.findCount();
        return list;
    }

    @RequestMapping("/orderDelect")
    @ResponseBody
    public String orderDelect(int[] ids) {
        orderService.deleteOrder(ids);
        return "gyy";
    }

    @RequestMapping("/todelect")
    @ResponseBody
    public List<CourseOrder> todelect(int ids) {
        int[] array = {ids};
        orderService.deleteOrder(array);
        List<CourseOrder> courseOrderList = orderService.findByCourse();
        return courseOrderList;
    }


    /**
     * 在clubinfo页面中预约课程
     * @param order
     * @return
     */
    @RequestMapping("/courseorderinsert")
    public String courseorderinsert(@ModelAttribute Order order, HttpSession session){
        Login login=(Login) session.getAttribute("currentLogin");
        int lId=login.getlId();
        System.out.println(lId);
        order.setlId(lId);
        order.setoStatus("已预约");
        orderService.insert(order);
        User user=userService.findById(lId);
        user.setNumber(user.getNumber()+1);
        userService.update(user);
        return "redirect:/toclubinfo.action?clId="+order.getcId();
    }

    /**
     * 取消预约
     * @param order
     * @return
     */
    @RequestMapping("/courseorderdelete")
    public String courseorderdelete(@ModelAttribute Order order){
        User user=userService.findById(order.getlId());
        user.setNumber(user.getNumber()-1);
        userService.update(user);
        orderService.deleteByoId(order.getoId());
        return "redirect:/toCourseOrder.action";
    }






}
